const app = getApp()

Page({
  data: {
    userInfo: null,
    isDealer: false,
    isAdmin: false,
    isSales: false,
    menus: []
  },
  onLoad() {
    this.initUserData()
    this.initMenus()
  },
  initUserData() {
    // 确保globalData存在且有默认值
    const globalData = app.globalData || {}
    const { userType = 'customer', userInfo = { nickName: '用户' } } = globalData
    
    this.setData({
      userInfo,
      isDealer: userType === 'dealer',
      isAdmin: userType === 'admin',
      isSales: userType === 'sales'
    })
  },
  initMenus() {
    const baseMenus = [
      { name: '我的订单', url: '/pages/order/list', icon: 'order' },
      { name: '地址管理', url: '/pages/user/address', icon: 'location' }
    ]
    
    if (this.data.isDealer) {
      baseMenus.push({ name: '客户管理', url: '/pages/dealer/customers', icon: 'group' })
    }
    
    if (this.data.isAdmin || this.data.isSales) {
      baseMenus.push({ name: '业绩统计', url: '/pages/stats', icon: 'chart' })
    }
    
    this.setData({ menus: baseMenus })
  },
  contactService() {
    wx.makePhoneCall({
      phoneNumber: '400-123-4567'
    })
  },
  navigateTo(e) {
    const url = e.currentTarget.dataset.url
    if (url === '/pages/order/list') {
      wx.switchTab({
        url: url
      })
    } else {
      wx.navigateTo({url})
    }
  }
})